package com.ygnn.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ygnn.reggie.common.R;
import com.ygnn.reggie.dto.DishDto;
import com.ygnn.reggie.entity.Category;
import com.ygnn.reggie.entity.Dish;
import com.ygnn.reggie.entity.DishFlavor;
import com.ygnn.reggie.entity.Setmeal;
import com.ygnn.reggie.service.CategoryService;
import com.ygnn.reggie.service.DishFlavorService;
import com.ygnn.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

//    新增菜品和口味
    @PostMapping
    public R<String> save(@RequestBody DishDto dishdto){
        log.info(dishdto.toString());
        dishService.saveWithFlavor(dishdto);
        return R.success("添加成功");
    }


//    分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){

        Page<Dish> pageInfo=new Page<>(page,pageSize);

        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper();

        queryWrapper.like(name!=null,Dish::getName,name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);

        dishService.page(pageInfo,queryWrapper);


        //        创建dishDtoPage
        Page<DishDto> dishDtoPage=new Page<>(page,pageSize);

//        拷贝除了records外的所有数据
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

//        处理dishDtoPage中的records
        List<DishDto> list=pageInfo.getRecords().stream().map((item)->{

            DishDto dishDto=new DishDto();
//            设置dishDto中的普通数据
            BeanUtils.copyProperties(item,dishDto);
//            得到categoryId
            Long categoryId = item.getCategoryId();
//            查询category表
            Category category = categoryService.getById(categoryId);
            if (category!=null) {
                String categoryName = category.getName();
//            设置dishDto中的categoryName
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(list);

        return R.success(dishDtoPage);

    }

//    回显
    @GetMapping("/{id}")
    public R<DishDto> get (@PathVariable Long id){
        DishDto dishDto = dishService.selectByIdWithFlavor(id);
        return R.success(dishDto);
    }


//   修改菜品和口味
    @PutMapping
    public R<String> update(@RequestBody DishDto dishdto){
        log.info(dishdto.toString());

        dishService.updateWithFlavor(dishdto);

        return R.success("添加成功");
    }


//    批量停售/起售
    @PostMapping("/status/{st}")
    public R<String> stopOrStart(@PathVariable int st, String ids)
    {
//        处理String转化成Long型
        String[] split=ids.split(",");
        List<Long> idList= Arrays.stream(split).map(item->{
            long l = Long.parseLong(item);
            return l;
        }).collect(Collectors.toList());

       log.info(idList.toString());

       List<Dish> dishes=idList.stream().map((item)->{
           Dish dish=new Dish();
           dish.setId(item);
           dish.setStatus(st);
           return dish;
       }).collect(Collectors.toList());

        dishService.updateBatchById(dishes);
        return R.success("操作成功");
    }

//    批量删除
    @DeleteMapping
    public R<String> delete(String ids){
        //        处理String转化成Long型
        String[] split=ids.split(",");
        List<Long> idList= Arrays.stream(split).map(item->{
            long l = Long.parseLong(item);
            return l;
        }).collect(Collectors.toList());

        log.info("删除的ids为:{}",idList);
//        删除dish表中数据
        dishService.removeByIds(idList);

//        删除dish_flavor表中数据

        for (int i = 0; i < idList.size(); i++) {
            LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,idList.get(i));
            dishFlavorService.remove(queryWrapper);
        }

        return R.success("删除成功");
    }


//       根据分类id查询对应菜品数据
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        queryWrapper.eq(Dish::getStatus,1);

        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(queryWrapper);

        List<DishDto> DtoList=list.stream().map((item)->{

            DishDto dishDto=new DishDto();
//            设置dishDto中的普通数据
            BeanUtils.copyProperties(item,dishDto);
//            得到categoryId
            Long categoryId = item.getCategoryId();
//            查询category表
            Category category = categoryService.getById(categoryId);
            if (category!=null) {
                String categoryName = category.getName();
//            设置dishDto中的categoryName
                dishDto.setCategoryName(categoryName);
            }
//            设置口味数据
            Long id = item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId,id);
            List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);

            dishDto.setFlavors(dishFlavorList);

            return dishDto;
        }).collect(Collectors.toList());


        log.info("DtoList{}",DtoList);


        return R.success(DtoList);

    }




}


















